using Microsoft.AspNetCore.Mvc;
using ManageSystem.Model;
using System.Threading.Tasks;
using ManageSystem.Service;
using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;
using System;


namespace ManageSystem.Controllers
{
    /// <summary>
    /// ADD and manage outlaystream
    /// </summary>
    [Route("ManageSystem/[controller]/[action]")]
    [ApiController]
    [Produces("application/json")]
    public class SystemSettingController : ControllerBase
    {
        public SystemSettingService systemSettingService = new();


        ///<summary>
        ///添加系统设置
        ///</summary>
        ///<param name="setting"></param>
        ///<returns></returns>
        [HttpPost]
        // [Authorize(Policy = "Permission")]
        public async Task<IActionResult> AddSystemSetting(SystemSetting setting)
        {
            try
            {
                Console.WriteLine(setting.guid);
                await systemSettingService.Add(setting);
                return Ok(setting.guid);
            }
            catch (Exception e)
            {
                return BadRequest(e.Message);
            }
        }

        ///<summary>
        /// 获取系统设置
        ///</summary>
        ///<param name="guid"></param>
        ///<returns></returns>
        [HttpGet]
        // [Authorize(Policy = "Permission")]
        public async Task<IActionResult> QuerySystemSetting(string guid)
        {
            try
            {
                SystemSetting setting = await systemSettingService.QueryByPrimaryKey(guid);
                return Ok(setting);
            }
            catch (Exception e)
            {
                return BadRequest(e.Message);
            }
        }

        ///<summary>
        ///更新和保存系统设置
        ///</summary>
        ///<param name="setting"></param>
        ///<returns></returns>
        [HttpPut]
        // [Authorize(Policy = "Permission")]
        public async Task<IActionResult> UpdateSystemSetting(SystemSetting setting)
        {
            try
            {
                await systemSettingService.Update(setting);
                return Ok("保存成功");
            }
            catch (Exception e)
            {
                return BadRequest(e.Message);
            }
        }

    }
}
